<?

/*	ICAL.basic OBJECT
*
*	Methods:
*	output	-> html string
*	ajax	-> javascript string
*	
*/

// include of the iCalcreator class (external class)
require_once ROOT . 'engine/ext/ical/iCalcreator.class.php';

class IcalBasic extends MOD {

	public function output() {
		global $Object, $Cal, $CAL;
		
		$CAL = new CALENDAR();
				
		// check upon action (single - day - default)
		if ( $this->action == "auto" ) {
			if ( $Object )		$this->action = "single";
			else if ( $Cal )	$this->action = "day";
			else 				$this->action = "default"; 
		}
		if ( $this->action == "day" ){
			$this->action = "day";
		}	

		// filter action (db)
		if ( $this->action ) $nodes = $this->{ "filter_" . $this->action }();
		
		// add css & show variables
		if ( $nodes ) {
			$this->addclass( $this->action );
			$result = implode( "\n", $nodes );
		}
		
		return ( $nodes )? $this->parse( $result ): '';
		
	}

		
	// single item print
	private function filter_single() {
		global $DB, $CAL, $Object;
				
		// extra date variable
		$nodes  = $CAL->filter_nodes_by_object('cal.single', $Object->type, (int) $Object->id);
		
		if ( $nodes ) {
			// return
			$node	= array_pop($nodes);
			$lines[] = $this->render_icon( "?action=single&type=" . $DB->type( $Object->type ) . "&ID=" . $Object->id . "&mod=" . $this->modid . "", $node->timestamp() );
			return $lines;
		} 
	}
	
	
	// day items print
	private function filter_day() {
		global $DB, $CAL, $Object;
		
		// extra date variable
		$nodes  = $CAL->filter_nodes_by_object('cal.single', $Object->type, (int) $Object->id);
		$node	= array_pop($nodes);
		$date	= date( "YmdHis", $node->timestamp() );
				
		// return
		$lines[] = $this->render_icon( "?action=day&day=" . $date . "&type=" . $DB->type( $Object->type ) . "&ID=" . $Object->id . "&mod=" . $this->modid . "", $node->timestamp() );		
		return $lines;
	}

	
	// all items print
	private function filter_type() {
		
		// return output
		$lines[] = $this->render_icon( "?action=type&type=" . $this->vars->type . "&mod=" . $this->modid . "");		
		return $lines;
	}


	// all items print
	private function filter_default() {
		
		// return output
		$lines[] = $this->render_icon( "?action=all&mod=" . $this->modid . "");		
		return $lines;
	}

	// render rss icon
	public function render_icon( $var, $date = '' ) {	
	
		// date variables
		$day = date('j', time()); $month = date('M', time());
		if (!empty($date)) {
			$day = date('j', $date); 
			$month = date('M', $date);
		}
		
		// return (vervang gentjazz.local!!)
		$return = "<a href='webcal://" . $_SERVER['SERVER_NAME'] . "/calendar.ical" . $var ."' class='ical'>";
		$return.= "<div class='month'>" . $month . "</div>" . $day . "</a>";
		return $return;
	}
	
} 

?>